//Event Entity
var Event = me.InvisibleEntity.extend(
    {
        init:function (x, y, settings)
        {

            x = Math.round(x / 16);
            y = Math.round(y / 16);

            // grabbin the event region
            var endX = x+Math.round((settings.width)/16)-1;
            var endY = y+Math.round((settings.height)/16)-1;

            //alert("ev no "+x + ' - '+y);
            alert("lendo "+settings.evType);
            if(true) { // if someone has implemented
               // alert("registrado");
                // loading the event
                if(!LoadedEvents.has(settings.evType)) {
                    // including the event to the main script
                    include('Events/'+settings.evType+'.js');
                    // as this event as already loaded
                    LoadedEvents.push(settings.evType)
                    //alert("loadei o event");
                }
                //alert("AKI");
                EventHandler["PlayerTeleportEvent"](1,2,3);
                if(EventHandler[settings.evType] != undefined)  {
                    // add as a running event from the map
                    RunningEvents.push({
                        evName : settings.evType,
                        evX :x,
                        evY :y,
                        evEndX : endX,
                        evEndY : endY,
                        evSettings : settings
                    });
                    alert("ev no "+x + ' - '+y);
                }

                //EventHandler[settings.evType](x,y,settings);
            }

            this.parent(x, y , settings);
        },
        // Update player position.
        update : function ()
        {
        }

 });



////// IMPLEMENTAR QUAD TREE AKI DPS /////
function searchForEvent(tileX, tileY) {

    var ct = RunningEvents.length;
    alert("procurando em "+tileX + " - "+tileY+ " ct"+ct);
    // for each event
    while(ct--) {
        //check if im inside that event
        if(tileX > RunningEvents[ct].x && tileX < RunningEvents[ct].endX)
            if(tileX > RunningEvents[ct].x && tileX < RunningEvents[ct].endX)
                // run the event
                EventHandler[RunningEvents[ct].evName](RunningEvents[ct]);
    }
}

// other events will add functions ('methods') to run theyr events here
var EventHandlerClass = function() {

}
var EventHandler = new EventHandlerClass(); // from where i will add the events

var RunningEvents = new Array(); // running events on the current map

var LoadedEvents = new Array(); // all loaded events (only once here)

// MODO FODA DE PROCURAR UM ITEM DENTRO DE UMA ARRAY
// QUE MEROS MORTAIS COMO NÒS NUNCA ENTENDEREMOS
// agora podemos usar minhaArray.has( kkerMerda );
Array.prototype.has = function(b){
    return!!~this.indexOf(b); // WTF ?
};

function include(filename)
{
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.src = filename;
    script.type = 'text/javascript';

    head.appendChild(script)
}